<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/7/1
 * Time: 7:33
 */

namespace app\home\controller;


use app\common\model\UserModel;
use app\common\model\UserSiteModel;
use think\Cookie;
use think\Request;

class ShopController extends BaseController
{
    private $userSiteModel;
    private $userModel;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->userSiteModel = new UserSiteModel();
        $this->userModel = new UserModel();
    }

    public function index()
    {
        $uid = $this->request->param('uid', 0);
        $info = $this->userModel->getInfo('site', $uid);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 店铺列表
     * 排序方式：default:店铺排序+店铺等级排序，grade:店铺等级降序,distance:距离远近排序，注意此排序需要定位支持
     * @return mixed|\think\Paginator
     */
    public function itemList()
    {
        $sort = $this->request->param('order', 'default');
        $order = 'site_sort grade_id desc';
        switch ($sort) {
            case 'grade':
                $order = 'grade_id desc';
                break;
        case 'distance':
                if (Cookie::has('lng') && Cookie::has('lat')) {
                    $order = 'ACOS(SIN((' . Cookie::get('lat') . ' * 3.1415) / 180 )*SIN((lat * 3.1415) / 180 ) +COS((' . Cookie::get('lat') . ' * 3.1415)/ 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . Cookie::get('lng') . ' * 3.1415)/ 180 - (lng * 3.1415) / 180 ) ) * 6380  asc';
                } else {
                    $this->error('您尚未定位暂时不能为您提供距离排序服务');
                }
                break;
            default:
                $order = 'site_sort grade_id desc';
        }
        $list = site_page_list(10, [], $order);
        if (Cookie::has('lng') && Cookie::has('lat')) {
            foreach ($list as $index => $value) {
                if (!empty($value['lng']&&!empty($value['lat']))){
                    $list[$index]['distance']=getDistance(\cookie('lat'),\cookie('lng'),$value['lat'],$value['lng']).'km';
                }else{
                    $list[$index]['distance']='未知';
                }
            }
        }else{
            foreach ($list as $index => $value) {
                $list[$index]['distance']='未知';
            }
        }
        if ($this->request->isPost()) {
            return $list;
        }
        $this->assign('itemList', $list);
        return $this->fetch();
    }
}